home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Magazine / Online / httpproxy / updateCache.rexx < prev   
OS/2 REXX Batch file  |  1996-08-20  |  6KB  |  246 lines

  1. /* Update the cache directory. */
  2. /* Usage: rx updateCache.rexx CACHEDIRNAME/A DODELETES/S*/
  3. /* CACHEDIRNAME: cache directory name
  4.  * DODELETES:    delete old directory */
  5.  
  6. /* All Files will converted either
  7.  * a) from old style to new style or
  8.  * b) from new style(1) to new style(2) (a newer one)
  9.  */
  10. /* Only security checks:
  11.  * a) existence of '.iscachedir'
  12.  * b) cache version number residing in @cachever */
  13. /* !!! httpresolve and delete have to be found in ram: !!!
  14.  * for mode b) old_httpresolve has to be found in ram:, too. */
  15.  
  16. parse arg dir dodeletes
  17. dodeletes = upper(dodeletes)
  18. address command
  19. options failat 21
  20.  
  21. if ~show('L','rexxarplib.library') then call addlib('rexxarplib.library',0,-30,0)
  22. if ~show('L','rexxsupport.library') then call addlib('rexxsupport.library',0,-30,0)
  23.  
  24. say "This script might generate some dos errors. Please ignore..."
  25.  
  26. /* create dir arguments */
  27. if right(dir,1) = ":" then
  28. do
  29.    olddir = dir
  30.    newdir = dir || "@new"
  31. end
  32. else
  33. do
  34.    olddir = dir || "/"
  35.    newdir = dir || "@new"
  36.    if right(dir,1) = "/" then
  37.    do
  38.       olddir = dir
  39.       newdir = left(dir, length(dir)-1) || "@new"
  40.    end
  41. end
  42. say "cache directory: "olddir " --- temporary new directory: "newdir"/"
  43.  
  44. /* Conversion style check:   style = 1: a)   style = 2: b) */
  45. style = 0
  46. if exists(olddir".iscachedir") then
  47.    style = 1
  48. else
  49. do
  50.    if exists(olddir"@dirurl") then
  51.    do
  52.       style = 2
  53.       if exists(olddir"@cachever") then
  54.       do
  55.      if ~ open('s', olddir"@cachever") then
  56.         exit 20
  57.      cachever = readln('s')
  58.      call close('s')
  59.      "ram:old_httpresolve >t:httpproxy_ver version"
  60.      if ~ open('s', "t:httpproxy_ver") then
  61.         exit 20
  62.      oldver = readln('s')
  63.      call close('s')
  64.      "ram:httpresolve >t:httpproxy_ver version"
  65.      if ~ open('s', "t:httpproxy_ver") then
  66.         exit 20
  67.      newver = readln('s')
  68.      call close('s')
  69.      "delete t:httpproxy_ver"
  70.      if (cachever = newver) then
  71.      do
  72.         say "*** cache dir does not need any conversion. ***"
  73.         exit 0
  74.      end
  75.      if (cachever ~= oldver) then
  76.      do
  77.         say "*** cache version and old httpresolve utility not equal -> cannot update cache ***"
  78.         exit 20
  79.      end
  80.       end
  81.    end
  82. end
  83. if (style = 0) then
  84. do
  85.    say "*** not a cache directory. exiting. ***"
  86.    exit 20
  87. end
  88.  
  89. /* Consistency checks */
  90. if ~exists("ram:httpresolve") then
  91.    "copy quiet amitcp:bin/httpresolve amitcp:bin/old_httpresolve ram:"
  92. if ~exists("ram:httpresolve") then
  93. do
  94.    say "*** error: cannot find httpresolve in ram: ***"
  95.    exit 20
  96. end
  97. if (style = 2) & ~exists("ram:old_httpresolve") then
  98. do
  99.    say "*** error: cannot find old httpresolve in ram: as old_httpresolve ***"
  100.    exit 20
  101. end
  102. "copy c:delete ram:"
  103.  
  104. /* generate new cache directory */
  105. if exists(left(olddir, length(olddir)-1)".old") then
  106.    "ram:delete "left(olddir, length(olddir)-1)".old all quiet"
  107. if exists(newdir) then
  108.    "ram:delete "newdir" all quiet"
  109. "makedir "newdir
  110. newdir = newdir || "/"
  111.  
  112. /* Generate special files and directories */
  113. "echo >"newdir"@dirurl"
  114. "makedir "newdir"@temp "newdir"@trash"
  115.  
  116. /* open move script for all cache files related to some URL files */
  117. if ~ open('s', "t:httpproxy_script", w) then
  118. do
  119.    say "*** error: cannot open temporary file ***"
  120.    exit 10
  121. end
  122. call writeln('s',"failat 21")
  123. call writeln('s',"cd "newdir)          /* needed for httpresolve */
  124.  
  125. /* generate file list */
  126. say "analysing directory..."
  127. lastdir = pragma(D, olddir)
  128. if style = 1 then
  129. do
  130.    NumFiles = FileList("@????????.????????", FList, F, N)
  131. end
  132. else
  133. if style = 2 then
  134. do
  135.    todo   = 1                          /* non-rekursive rekursive file tree walker */
  136.    Dirs.1 = ""
  137.    i = 1
  138.    do while todo > 0
  139.       CurDir = Dirs.todo
  140.       Num = FileList(CurDir"#?", IList, D, N)
  141.       do j = 1 to Num
  142.      Dirs.todo = CurDir || IList.j"/"
  143.      todo = todo + 1
  144.       end
  145.       todo    = todo - 1
  146.       Num = FileList(CurDir"#?@", IList, F, N)
  147.       do j = 1 to Num
  148.      if substr(IList.j, 1, 2) ~= "@@" then
  149.      do
  150.         FList.i = CurDir || IList.j
  151.         i = i + 1
  152.      end
  153.       end
  154.    end
  155.    NumFiles = i - 1
  156. end
  157. realdir = pragma(D, lastdir)
  158. if (right(realdir,1) ~= "/") & (right(realdir,1) ~= ":") then
  159.    realdir = realdir || "/"
  160.  
  161. /* generate move script */
  162. say "generating move script..."
  163. if style = 1 then
  164. do
  165.    do i=1 to NumFiles
  166.       if substr(FList.i, 1, 1) ~= "@" then
  167.      exit 10
  168.       if exists(realdir"_"substr(FList.i, 2)) then
  169.       do
  170.      if open('f', realdir || FList.i, R) then
  171.      do
  172.         url = readln('f')
  173.         call close('f')
  174.         if (url ~= "") then
  175.         do
  176.            call writeln('s',"ram:httpresolve <"""realdir"_"substr(FList.i, 2)""" SAVE URL """url"""")
  177.            if (dodeletes = "DODELETES") then
  178.           call writeln('s',"ram:delete quiet file """realdir"_"substr(FList.i, 2)"""")
  179.         end
  180.      end
  181.       end
  182.    end
  183. end
  184. else
  185. if style = 2 then
  186. do
  187.    call pragma(D, olddir)                /* needed for old httpresolve */
  188.    do i=1 to NumFiles
  189.       "ram:old_httpresolve >t:httpproxy_result file """FList.i""""
  190.       if open('f', "t:httpproxy_result", R) then
  191.       do
  192.      url = readln('f')
  193.      call close('f')
  194.      if (url ~= "") then
  195.      do
  196.         call writeln('s',"ram:httpresolve <"""realdir || FList.i""" SAVE URL """url"""")
  197.         if (dodeletes = "DODELETES") then
  198.            call writeln('s',"ram:delete quiet file """realdir || FList.i"""")
  199.      end
  200.       end
  201.    end
  202. end
  203.  
  204. /* perform move action */
  205. say "performing moves..."
  206. call close('s')
  207. call pragma(D, lastdir)
  208. "execute t:httpproxy_script"
  209.  
  210. call pragma(D, newdir)
  211. "ram:httpresolve >@cachever version"
  212. call pragma(D, lastdir)
  213.  
  214. "c:copy msg "newdir"@msg all"          /* copy message files */
  215.  
  216. /* Delete old special files, url files and remaining (unrelated) cache files */
  217. "ram:delete >nil: t:httpproxy_result t:httpproxy_script"
  218. if dodeletes = "DODELETES" then
  219. do
  220.    say "deleting old files..."
  221.    "ram:delete >nil: "olddir".#?"
  222.    if style = 1 then
  223.    do
  224.       "ram:delete >nil: "olddir"@?????????????????"
  225.       "ram:delete >nil: "olddir"_?????????????????"
  226.    end
  227.    else
  228.       "ram:delete >nil: "olddir"~(@new) all"
  229. end
  230.  
  231. /* Try to delete old cache directory but keep in case of non-emptyness.. */
  232. "ram:delete "left(olddir, length(olddir)-1) "quiet"
  233. if exists(left(olddir, length(olddir)-1)) then
  234.    "rename >nil: "left(olddir, length(olddir)-1) left(olddir, length(olddir)-1)".old"
  235. if right(olddir, 1) ~= ":" then
  236.    "rename "left(newdir, length(newdir)-1) left(olddir, length(olddir)-1)
  237. if exists(newdir) then
  238. do
  239.    "rename "newdir"#? "olddir "quiet"  /* move them to new directory */
  240.    "ram:delete "newdir "quiet"
  241. end
  242.  
  243. say "*** done. ***"
  244. exit 0
  245.  
  246.